package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <h3>springcloudSaas</h3>
 * <p>发布能力</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-29 20:43
 **/
@RestController
@RequestMapping("/freePublish")
public class MpFreePublishController {

    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;


    /**
     * 删除发布
     */
    @RequiresPermissions("mp:draft:deletePush")
    @Log(title = "删除发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult deletePush(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String articleId = "asdasd";
        Integer index = 1;
        wxService.switchoverTo(appId).getFreePublishService().deletePush(articleId, index);
        return AjaxResult.success();
    }


    /**
     * 删除所有发布
     */
    @RequiresPermissions("mp:draft:deletePushAllArticle")
    @Log(title = "删除所有发布", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteAll/{id}")
    public AjaxResult deletePushAllArticle(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String articleId = "asdasd";
        wxService.switchoverTo(appId).getFreePublishService().deletePushAllArticle(articleId);
        return AjaxResult.success();
    }


    /**
     * 通过 article_id 获取已发布文章
     */
    @RequiresPermissions("mp:draft:getArticleFromId")
    @Log(title = "通过 article_id 获取已发布文章", businessType = BusinessType.EXPORT)
    @GetMapping("getArticleFromId/{id}")
    public AjaxResult getArticleFromId(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String articleId = "asdasd";
        wxService.switchoverTo(appId).getFreePublishService().getArticleFromId(articleId);
        return AjaxResult.success();
    }


    /**
     * 获取成功发布列表 - 支持选择是否返回：图文消息的具体内容
     */
    @RequiresPermissions("mp:draft:getPublicationRecords")
    @Log(title = "获取成功发布列表", businessType = BusinessType.EXPORT)
    @GetMapping("getPublicationRecords/{id}")
    public AjaxResult getPublicationRecords(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        int offset = 1;
        int count = 10;
        int noCont = 20;
        wxService.switchoverTo(appId).getFreePublishService().getPublicationRecords(offset,count,noCont);
        return AjaxResult.success();
    }

    /**
     * 发布状态轮询接口
     */
    @RequiresPermissions("mp:draft:getPushStatus")
    @Log(title = "发布状态轮询接口", businessType = BusinessType.EXPORT)
    @GetMapping("getPushStatus/{id}")
    public AjaxResult getPushStatus(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String publishId = "asdasd";
        wxService.switchoverTo(appId).getFreePublishService().getPushStatus(publishId);
        return AjaxResult.success();
    }

    /**
     * 发布接口
     */
    @RequiresPermissions("mp:draft:submit")
    @Log(title = "发布接口", businessType = BusinessType.INSERT)
    @PostMapping("/submit/{id}")
    public AjaxResult submit(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String mediaId = "asdasd";
        wxService.switchoverTo(appId).getFreePublishService().submit(mediaId);
        return AjaxResult.success();
    }
}


